﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class cp_Charts_Hours : System.Web.UI.Page
{
    public string Data = "";
    public int TimezoneDifference = 0;
    DateTime startTime;
    DateTime endTime;
    

    protected void Page_Load(object sender, EventArgs e)
    {
        int thermostatId=Convert.ToInt32(Request["thermostatId"]);
        TimezoneDifference = Convert.ToInt32(Request["TimezoneDifference"]);

        ThermostatMonitorLib.Cycles cycles = ThermostatMonitorLib.Cycles.LoadRange(thermostatId, new DateTime(2000, 1, 1), DateTime.Now);



        if (cycles.Count < 2) return;
        startTime = cycles[0].StartDate;
        endTime = cycles[cycles.Count - 1].EndDate;
        if (endTime==DateTime.MinValue) endTime = cycles[cycles.Count - 2].EndDate;

        System.Collections.Hashtable ht = new System.Collections.Hashtable();
        for (int i = 0; i < 23; i++) ht[i] = 0;

        foreach (ThermostatMonitorLib.Cycle c in cycles)
        {
            if (!c.IsEndDateNull && c.CycleType=="Cool")
            {
                int startHour = c.StartDate.Hour + TimezoneDifference;
                if (startHour < 0) startHour = startHour + 24;
                if (startHour > 23) startHour = startHour - 24;

                int endHour = c.EndDate.Hour + TimezoneDifference;
                if (endHour < 0) endHour = endHour + 24;
                if (endHour > 23) endHour = endHour - 24;


                if (startHour == endHour)
                {
                    ht[startHour] = Convert.ToInt32(ht[startHour]) + new TimeSpan(c.EndDate.Ticks - c.StartDate.Ticks).TotalMinutes;
                }
                else
                {
                    ht[startHour] = Convert.ToInt32(ht[startHour]) + new TimeSpan(new DateTime(c.StartDate.Year, c.StartDate.Month, c.StartDate.Day, c.StartDate.Hour, 0, 0).AddHours(1).Ticks - c.StartDate.Ticks).TotalMinutes;
                    ht[endHour] = Convert.ToInt32(ht[endHour]) + new TimeSpan(c.EndDate.Ticks - new DateTime(c.EndDate.Year, c.EndDate.Month, c.EndDate.Day, c.EndDate.Hour, 0, 0).Ticks).TotalMinutes;
                }
            }
        }

        Output(ht);
    }


    private void Output(Hashtable ht)
    {
        int days = Convert.ToInt32(new TimeSpan(endTime.Date.Ticks - startTime.Date.Ticks).TotalDays)-1;

        //FillInDataTable(dt);

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("data.addRows(24);\n");
        for (int i=0;i<24;i++)
        {
            int tempDays = days;
            if (startTime.Hour < i) tempDays++;
            if (endTime.Hour > i) tempDays++;
            int averageMinutes = Convert.ToInt32(Convert.ToDouble(ht[i]) / tempDays);

            string displayTime = "";
            if (i < 13) displayTime = i.ToString() + "am"; else displayTime = Convert.ToInt32(i - 12).ToString() + "pm";
            if (displayTime == "12am") displayTime = "12pm";
            if (displayTime == "0am") displayTime = "12am";




            sb.Append("data.setValue(" + i.ToString() + ",0,'" + displayTime + "');\n");
            sb.Append("data.setValue(" + i.ToString() + ",1," + averageMinutes.ToString() + ");\n");
        }


        Data = sb.ToString();
        Data += "var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));\n";
        Data += "chart.draw(data, {width: 970, height: 200, scaleType:'maximized', legend:'none', hAxis: { showTextEvery:2 }, chartArea: { width:935 } });";
        ChartDiv.Text = "<div id=\"chart_div\" style=\"width: 970px; height: 200px;\"></div>";
    }

}
